<?php

class NDocumentRendererPortfolioDetail extends NDocumentRenderer
{
	
	function render($name = null, $params = array (), $content = null)
	{
	if (NRequest::getVar('component','com_catalog') && NRequest::getVar('view','details') && NRequest::getVar('id')){ $firm_id=NRequest::getVar('id');NRequest::setVar('firm_id',$firm_id);}
	$db = NFactory::getDBO();
	// список всех портволио в структуре
	ob_start();
	$_sql = "SElECT p.*,pi.image  FROM #__portfolio as p JOIN #__portfolio_image as pi on p.default_image=pi.id WHERE firm_id=".$firm_id;

	$db->setQuery($_sql);
	$oPortfolios = $db->loadObjectList();
	if (!$oPortfolios) return; // no portfolio

// Самая первая фотка в структуре
	if (!NRequest::getVar('portfolioitem')){
	NRequest::setVar('portfolioitem',$oPortfolios[0]->default_image);
	}
	$_sql = "SElECT pi.id,pi.image,pi.portfolio_id ,p.structure_id,p.title as portfolio_title,p.shortbody as portfolio_shortbody FROM #__portfolio_image  pi  JOIN  #__portfolio p on pi.portfolio_id=p.id WHERE pi.id=".NRequest::getVar('portfolioitem');
	$db->setQuery($_sql,0,1);
	
	$oItem = $db->loadObject();
	$oItem->imagesize=getimagesize(ROOT_DIR."/uploads/com_portfolio/380x_".$oItem->image);
// все изображения в портфолио
	$_sql = "SElECT pi.id,pi.image,pi.portfolio_id ,p.structure_id FROM #__portfolio_image  pi  JOIN  #__portfolio p on pi.portfolio_id=p.id WHERE pi.portfolio_id=".$oItem->portfolio_id;
	//echo $db->replacePrefix($_sql);
	$db->setQuery($_sql);
	$oPortFolioImages=$db->loadObjectList();
	$oTpl = NTemplateEngine::getInstance();
	$oTpl->set('oPortfolios',$oPortfolios);
	$oTpl->set('oCurrImage',$oItem);
	$oTpl->set('oPortFolioImages',$oPortFolioImages);
	echo $oTpl->render(dirname(__FILE__)."/tmpl/portfolio.phtml");

	$content = ob_get_contents();
	ob_clean();
	return  $content;
	}
}